package cn.itcast.h_parallelGateWay;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

public class ParallelGateWayTest {

	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	/**
	 * 部署流程定义
	 * 1
	 */
	@Test
	public void deploymentProcessDefinition_inputStrem(){
		InputStream sqb=this.getClass().getResourceAsStream("parallelGateWay.bpmn");
		InputStream sqp=this.getClass().getResourceAsStream("parallelGateWay.png");
		
		
		Deployment deployment = processEngine.getRepositoryService()
		.createDeployment()//创建一个部署对象
		.name("并行网关")
		.addInputStream("parallelGateWay.bpmn", sqb)
		.addInputStream("parallelGateWay.png", sqp)
		.deploy();
		
		System.out.println("部署ID："+deployment.getId());//
		System.out.println("部署名称："+deployment.getName());//
	}
	/**
	 * 启动流程实例
	 * 2
	 */
	@Test
	public void startProcessInstance(){
		//流程定义的key
		String processDefinitionKey = "parallelGateWay";
		ProcessInstance pi = processEngine.getRuntimeService()//与正在执行的流程实例和执行对象相关的Service
						.startProcessInstanceByKey(processDefinitionKey);//使用流程定义的key启动流程实例，key对应helloworld.bpmn文件中id的属性值，使用key值启动，默认是按照最新版本的流程定义启动
		System.out.println("流程实例ID:"+pi.getId());//流程实例ID    101
		System.out.println("流程定义ID:"+pi.getProcessDefinitionId());//流程定义ID   helloworld:1:4
	}
	/**
	 * 查询当前人的个人任务
	 * 3
	 */
	@Test
	public void findMyPersonalTask(){
		String assignee = "买家";
		List<Task> list = processEngine.getTaskService()//与正在执行的任务管理相关的Service
						.createTaskQuery()//创建任务查询对象
						.taskAssignee(assignee)//指定个人任务查询，指定办理人
						.list();
		if(list!=null && list.size()>0){
			for(Task task:list){
				System.out.println("任务ID:"+task.getId());
				System.out.println("任务名称:"+task.getName());
				System.out.println("任务的创建时间:"+task.getCreateTime());
				System.out.println("任务的办理人:"+task.getAssignee());
				System.out.println("流程实例ID："+task.getProcessInstanceId());
				System.out.println("执行对象ID:"+task.getExecutionId());
				System.out.println("流程定义ID:"+task.getProcessDefinitionId());
				System.out.println("########################################################");
			}
		}
	}
	/**
	 * 完成我的任务
	 * 4
	 */
	@Test
	public void completeMyPersonalTask(){
		//任务ID
		String taskId = "4002";
		//完成任务的同事设置流程变量,使用流程变量来设置下一个连线的方向
		Map<String,Object> Variable=new HashMap<String,Object>();
		Variable.put("money", 400);
		
		processEngine.getTaskService()//与正在执行的任务管理相关的Service
					.complete(taskId, Variable);
		System.out.println("完成任务：任务ID："+taskId);
	}
}
